package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientResourcePool;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.TransferRecord;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.ClientResourcePoolMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.TransferRecordMapper;
import cn.wolfcode.crm.query.AssignObject;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {

    @Autowired
    private ClientMapper mapper;


    public void delete(Long id) {


        mapper.deleteByPrimaryKey(id);
    }

    public void save(Client entity) {

        //获取当前用户
        Employee marketer =  (Employee) SecurityUtils.getSubject().getPrincipal();
        entity.setMarketer(marketer);
        Employee inputer = (Employee) SecurityUtils.getSubject().getPrincipal();
        entity.setInputer(inputer);
        entity.setInputTime(new Date());
        entity.setLastTraceTime(new Date());
        entity.setFilingDate(new Date());//建档时间

        List<Client> clients = mapper.selectAll();
        for (Client client : clients) {

            if (entity.getName().equals(client.getName())) {
                throw new RuntimeException("该客户已存在");
            }
        }

        mapper.insert(entity);
    }
    public void update(Client entity) {

        Employee marketer =  (Employee) SecurityUtils.getSubject().getPrincipal();
        entity.setMarketer(marketer);
        Employee inputer = (Employee) SecurityUtils.getSubject().getPrincipal();
        entity.setInputer(inputer);
        entity.setInputTime(new Date());
        entity.setLastTraceTime(new Date());


        mapper.updateByPrimaryKey(entity);

    }

    public Client get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Client> list() {
        return mapper.selectAll();
    }

    //高级查询
    public PageResult query(ClientQueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<Client> list = mapper.queryForList(qo);
        return new PageResult(total,list);
    }

    @Autowired
    private ClientResourcePoolMapper clientResourcePoolMapper;


    //将客户放入资源池
    public void putPool(Long id) {

        ClientResourcePool pool = new ClientResourcePool();
        pool.setClient(mapper.selectByPrimaryKey(id));

        //放入资源池
        clientResourcePoolMapper.insert(pool);

        //修改是否在资源池状态
        mapper.updateInPool(id);
    }


    @Autowired
    private TransferRecordMapper recordMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    //分派客户资源
    public void sign(AssignObject ao) {

        //移交记录对象
        TransferRecord transferRecord = new TransferRecord();

        //获取客户对象
        Client client = mapper.selectByPrimaryKey(ao.getClientId());

        transferRecord.setClient(client);

        transferRecord.setDate(new Date());

        Employee oldTracer = client.getTracer();
        transferRecord.setOldTracer(oldTracer);

        //如果是主管移交
        if (ao.getTracerId() != null) {
            Employee newEmployee = employeeMapper.selectByPrimaryKey(ao.getTracerId());
            transferRecord.setNewTracer(newEmployee);
        }else{
            transferRecord.setNewTracer((Employee) SecurityUtils.getSubject().getPrincipal());
        }
        
        mapper.updateInPool(client.getId());

        recordMapper.insert(transferRecord);


        mapper.sign(ao);

        clientResourcePoolMapper.deleteByPrimaryKey(ao.getPoolId());
    }

}
